/**
 * 长按指令
 *
 *  <button v-longpress="() => {}"></button>
 */

export default {
  mounted(el, binding) {
    let timer

    const start = (e) => {
      timer = setTimeout(() => {
        // 长按两秒之后，执行传递过来的函数
        binding.value(e)
      }, 2000)
    }

    const cancel = () => {
      console.log('cancel')
      // 计时器关闭
      timer && clearTimeout(timer)
    }

    el.addEventListener('mousedown', start)
    el.addEventListener('touchstart', start)

    el.addEventListener('mouseup', cancel)
    el.addEventListener('mouseout', cancel)
    el.addEventListener('touchend', cancel)
    el.addEventListener('touchcancel', cancel)
  }
}
